package org.szgb.console.sys.user.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.user.entity.SysUserRole;
import org.szgb.console.sys.user.mapper.SysUserRoleMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysUserRoleService {
    private final SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    public SysUserRoleService(SysUserRoleMapper sysUserRoleMapper) {
        this.sysUserRoleMapper = sysUserRoleMapper;
    }

    @Transactional(readOnly = true)
    public SysUserRole get(String id) {
        return sysUserRoleMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysUserRole> findList(SysUserRole sysUserRole) {
        if (sysUserRole != null && sysUserRole.getPageDto() != null) {
            Page<SysUserRole> page = new Page<>(sysUserRole.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysUserRole.setPage(page);
        }
        return sysUserRoleMapper.findList(sysUserRole);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysUserRole sysUserRole) {
        return sysUserRoleMapper.getCount(sysUserRole);
    }

    @Transactional()
    public void save(SysUserRole sysUserRole) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysUserRole.getId() == null || sysUserRole.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysUserRole.setCreatedBy(sysUserRole.getUpdatedBy());
            // 添加UUID
            sysUserRole.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysUserRoleMapper.insert(sysUserRole);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysUserRoleMapper.update(sysUserRole);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysUserRoleMapper.delete(ids.split(","));
    }
}
